#!/bin/bash

set -ux


### --start_docs
## Deploying the cell
## ==================

## Prepare Your Environment
## ------------------------

## * Source in the undercloud credentials.
## ::

source {{ working_dir }}/stackrc

### --start_docs

# Create directory for cell related parameter files
mkdir -p {{ working_dir }}/{{ cell_name }}

# export overcloud information needed as input for cell stack
openstack overcloud cell export {{ cell_name }} -o {{ working_dir }}/{{ cell_name }}/cell-input.yaml

# create cell roles file with Compute and CellController role
openstack overcloud roles generate --roles-path \
    /usr/share/openstack-tripleo-heat-templates/roles \
    -o {{ working_dir }}/{{ cell_name }}/cell_roles_data.yaml Compute CellController

## * Deploy the cell
## ::
openstack overcloud deploy {% if release is not in ['newton', 'ocata', 'pike', 'queens'] %}--override-ansible-cfg {{ working_dir }}/custom_ansible.cfg{% endif %} \
    --templates {{overcloud_templates_path}} \
    {{ deploy_args | regex_replace("\n", " ") }} \
    {{ cell_deploy_args | regex_replace("\n", " ") }} \
    "$@" && status_code=0 || status_code=$?

### --stop_docs

if ! openstack stack list | grep -q {{ cell_name }}; then
    echo "cell deployment not started. Check the deploy configurations"
    exit 1

    # We don't always get a useful error code from the openstack deploy command,
    # so check `openstack stack list` for a CREATE_COMPLETE or an UPDATE_COMPLETE
    # status.
elif ! openstack stack list | grep {{ cell_name }} | grep -Eq '(CREATE|UPDATE)_COMPLETE'; then
    {%if release not in ['mitaka', 'liberty'] %}
    # get the failures list
    openstack stack failures list overcloud --long > {{ failed_cell_deployment_list }} || true
    {% endif %}

    # get any puppet related errors
    for failed in $(openstack stack resource list \
        --nested-depth 5 {{ cell_name }} | grep FAILED |
        grep 'StructuredDeployment ' | cut -d '|' -f3)
    do
    echo "openstack software deployment show output for deployment: $failed" >> {{ failed_cell_deployments_log }}
    echo "######################################################" >> {{ failed_cell_deployments_log }}
    openstack software deployment show $failed >> {{ failed_cell_deployments_log }}
    echo "######################################################" >> {{ failed_cell_deployments_log }}
    echo "puppet standard error for deployment: $failed" >> {{ failed_cell_deployments_log }}
    echo "######################################################" >> {{ failed_cell_deployments_log }}
    # the sed part removes color codes from the text
    openstack software deployment show $failed -f json |
        jq -r .output_values.deploy_stderr |
        sed -r "s:\x1B\[[0-9;]*[mK]::g" >> {{ failed_cell_deployments_log }}
    echo "######################################################" >> {{ failed_cell_deployments_log }}
    # We need to exit with 1 because of the above || true
    done
    exit 1
{%if release in ['master'] %}
elif ! openstack overcloud status --stack {{ cell_name }}| grep -Eq 'DEPLOY_SUCCESS'; then
    # NOTE(emilien) "openstack overcloud failures" was introduced in Rocky
    openstack overcloud failures --stack {{ cell_name }}>> {{ failed_cell_deployment_list }} || true
    exit 1
{% endif %}
fi

# Create inventory files for overcloud and cell stack
mkdir -p {{ working_dir }}/inventories

for i in $(openstack stack list -f value -c 'Stack Name'); do
    /usr/bin/tripleo-ansible-inventory \
        --static-yaml-inventory inventories/${i}.yaml \
        --stack ${i}
done

ANSIBLE_HOST_KEY_CHECKING=False \
ANSIBLE_SSH_RETRIES=3 \
ansible-playbook -i inventories \
    /usr/share/ansible/tripleo-playbooks/create-nova-cell-v2.yaml \
    -e tripleo_cellv2_cell_name={{ cell_name }} \
    -e tripleo_cellv2_containercli={{ overcloud_container_cli }}

exit $status_code
